﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.22
 * 描 述：项目库
 */
var selectedRow;
var refreshGirdData;
var grid;

var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.bind();
            page.initleft();
            page.initGrid_a();
            page.initGrid_b();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 查看
            $('#lr_view').on('click', function () {
                var data_value = $('#lr_left_list').find('.active').attr('data-value');
                switch (data_value) {
                    case "1":
                        var keyValue = $('#gridtable_a').jfGridValue('id');
                        selectedRow = $('#gridtable_a').jfGridGet('rowdata');
                        break;
                    case "2":
                        var keyValue = $('#gridtable_b').jfGridValue('id');
                        selectedRow = $('#gridtable_b').jfGridGet('rowdata');

                        break;
                }
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '查看项目',
                        url: config.webportal_server + 'pages/pbd/project/view.html?access_token=' + config.getToken().access_token,
                        width: 1080,
                        height: 740,
                        btn: ['关闭']
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'pbdprojectas/delete/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });

            // 从excel导入
            $('#lr_import').on('click', function () {
                learun.layerForm({
                    id: 'UploadifyForm',
                    title: '上传文件',
                    url: config.webportal_server + 'pages/pbd/project/uploadifyForm.html?access_token=' + config.getToken().access_token,
                    width: 600,
                    height: 400,
                    callBack: function (id) {
                        return top[id].acceptClick(function (datas) {
                            if (datas) {
                                refreshGirdData();
                            }
                        });
                    }
                });
            });
            // 数据导出
            $('#lr_export').on('click', function () {
                location.href = config.base_server() + 'pbdprojects/exportExcel?access_token=' + config.getToken().access_token;
            });
        },
        initleft: function () {
            $('#lr_left_list li').on('click', function () {
                var $this = $(this);
                var $parent = $this.parent();
                $parent.find('.active').removeClass('active');
                $this.addClass('active');

                var data_value = $this.attr('data-value');

                switch (data_value) {
                    case "1":
                        grid = $("#gridtable_dz");
                        $("#gridtable_a").show();
                        $("#gridtable_b").hide();

                        break;
                    case "2":
                        grid = $("#gridtable_b");
                        $("#gridtable_a").hide();
                        $("#gridtable_b").show();

                        break;
                    default:
                        grid = $("#gridtable_a");
                        $("#gridtable_a").show();
                        $("#gridtable_b").hide();

                        break;
                }

                page.search();
            });
        },
        initGrid_a: function () {
            $('#gridtable_a').jfGrid({
                url: config.base_server() + 'pbdprojectas/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: '项目编码', name: 'code', width: 120, align: 'center'},
                    {label: '项目名称', name: 'name', width: 200, align: 'center'},
                    {label: '项目类别', name: 'category', width: 150, align: 'center'},
                    {label: '项目性质', name: 'nature', width: 150, align: 'center'},
                    {
                        label: '项目立项预算(元)', name: 'totalMoney', width: 180, align: 'center',
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {label: '项目属性', name: 'attribute', width: 150, align: 'center'},
                    {label: '立项部门', name: 'departmentName', width: 150, align: 'center'},
                    {label: '项目负责人', name: 'chargePerson', width: 100, align: 'center'},
                    {label: '项目执行年度', name: 'actionYear', width: 100, align: 'center'},
                    {label: '创建人', name: 'createUsername', width: 100, align: 'center'},
                    {
                        label: "创建时间", name: "createTime", width: 140, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {label: 'id', name: 'id', width: 100, align: 'left', hidden: true},
                    {label: 'applyBillid', name: 'applyBillid', width: 100, align: 'left', hidden: true},
                ],
                dblclick: function (row) {
                    var keyValue = $('#gridtable_a').jfGridValue('id');
                    selectedRow = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'form',
                            title: '查看项目',
                            url: config.webportal_server + 'pages/pbd/project/view.html?access_token=' + config.getToken().access_token,
                            width: 1080,
                            height: 740,
                            btn: ['关闭']
                        });
                    }
                },
                isPage: true,
                reloadSelected: true,
                mainId: 'id'
            });

            page.search();
        },
        initGrid_b: function () {
            $('#gridtable_b').jfGrid({
                url: config.base_server() + 'pbdprojectbs/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: '项目编码', name: 'code', width: 120, align: 'center'},
                    {label: '项目名称', name: 'name', width: 200, align: 'center'},
                    {label: '项目类别', name: 'category', width: 150, align: 'center'},
                    {label: '项目性质', name: 'nature', width: 150, align: 'center'},
                    {
                        label: '项目立项预算(元)', name: 'totalMoney', width: 180, align: 'center',
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {label: '项目属性', name: 'attribute', width: 150, align: 'center'},
                    {label: '立项部门', name: 'departmentName', width: 150, align: 'center'},
                    {label: '项目负责人', name: 'chargePerson', width: 100, align: 'center'},
                    {label: '项目执行年度', name: 'actionYear', width: 100, align: 'center'},
                    {label: '创建人', name: 'createUsername', width: 100, align: 'center'},
                    {
                        label: "创建时间", name: "createTime", width: 140, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {label: 'id', name: 'id', width: 100, align: 'left', hidden: true},
                    {label: 'applyBillid', name: 'applyBillid', width: 100, align: 'left', hidden: true},
                ],
                dblclick: function (row) {
                    var keyValue = $('#gridtable_b').jfGridValue('id');
                    selectedRow = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'form',
                            title: '查看项目',
                            url: config.webportal_server + 'pages/pbd/project/view.html?access_token=' + config.getToken().access_token,
                            width: 1080,
                            height: 740,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                isPage: true,
                reloadSelected: true,
                mainId: 'id'
            });

            page.search();
        },
        search: function (param) {
            param = param || {};
            param.keyword = $("#txt_Keyword").val();

            var data_value = $('#lr_left_list').find('.active').attr('data-value');
            switch (data_value) {
                case "1":
                    $('#gridtable_a').jfGridSet('reload', param);
                    break;
                case "2":
                    $('#gridtable_b').jfGridSet('reload', param);
                    break;
            }
        }
    };

    refreshGirdData = function () {
        page.search();
    };

    page.init();
}


